package com.guobox.movies.view.activity;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.guobox.movies.R;
import com.guobox.movies.constant.Constant;
import com.guobox.movies.entity.BgColors;
import com.guobox.movies.entity.FileItem;
import com.guobox.movies.entity.MovieEntity;
import com.guobox.movies.nasManager.NasManager;
import com.guobox.movies.usecase.PlayMovieModel;
import com.guobox.movies.usecase.PlayMoviesModelImp;
import com.guobox.movies.view.mview.BgView;

import java.util.List;

public class MovieSerialActivity extends Activity {

    BgView bgView;
    ListView listView;

    List<FileItem> fileItems;
    MovieEntity movieEntity;
    BgColors bgColors;

    PlayMovieModel playMovieModel = null;
    Gson gson = new Gson();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_serial);
        getData();
        initView();
    }

    void getData() {
        playMovieModel = new PlayMoviesModelImp(this);
//        fileItems = (List<FileItem>) getIntent().getSerializableExtra(Constant.NAS_FILE);
        String movieString = getIntent().getStringExtra(Constant.PLAY_MOVIE);
        bgColors = (BgColors) getIntent().getSerializableExtra(Constant.BG_COLORS);
        if (!TextUtils.isEmpty(movieString)) {
            movieEntity = gson.fromJson(movieString, MovieEntity.class);
        }
        if (movieEntity != null) {
            fileItems = movieEntity.getFiles();
        }

    }

    void initView() {
        bgView = (BgView) findViewById(R.id.wrapper_bg);
        listView = (ListView) findViewById(R.id.lv_movie_serial);

        bgView.setColos(bgColors);
        if (fileItems != null) {
            listView.setAdapter(new FilelistAdapter(fileItems));
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NasManager nasManager = new NasManager(getApplicationContext());
                String nasIp = nasManager.getNas().getNasIp();
                if (TextUtils.isEmpty(nasIp)) {
                    Toast.makeText(MovieSerialActivity.this, "无法连接到主机", Toast.LENGTH_SHORT).show();
                    return;
                }
                movieEntity.setHistory(new int[]{position});
                playMovieModel.playMovies(movieEntity,nasIp);
            }
        });
    }

    private class FilelistAdapter extends BaseAdapter {
        private List<FileItem> fileItems;

        FilelistAdapter(List<FileItem> fileItems) {
            this.fileItems = fileItems;
        }

        @Override
        public int getCount() {
            return fileItems == null ? 0 : fileItems.size();
        }

        @Override
        public Object getItem(int position) {
            return fileItems != null ? fileItems.get(position) : new FileItem();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FilesHolder filesHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_file_list, null);
                filesHolder = new FilesHolder(convertView);
                convertView.setTag(filesHolder);
            } else {
                filesHolder = (FilesHolder) convertView.getTag();
            }
            filesHolder.tvMovieName.setText(fileItems.get(position).getName());
            if (movieEntity.getHistory() != null && position == movieEntity.getHistory()[0]) {
                filesHolder.tvMovieName.setTextColor(bgColors.getSubTextColor());
                filesHolder.ivMovieIcon.setImageTintList(ColorStateList.valueOf(bgColors.getSubTextColor()));
            } else {
                filesHolder.tvMovieName.setTextColor(bgColors.getMainTextColor());
                filesHolder.ivMovieIcon.setImageTintList(ColorStateList.valueOf(bgColors.getMainTextColor()));
            }

            return convertView;
        }

        class FilesHolder {
            View itemView;
            TextView tvMovieName;
            ImageView ivMovieIcon;

            FilesHolder(View itemView) {
                this.itemView = itemView;
                tvMovieName = (TextView) itemView.findViewById(R.id.tv_movie_file_name);
                ivMovieIcon = (ImageView) itemView.findViewById(R.id.iv_movie_icon);
            }

        }
    }
}
